package com.micro.ai.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.micro.ai.commons.exception.BusinessException;
import com.micro.ai.template.entity.TemplateFavorite;
import com.micro.ai.template.mapper.TemplateFavoriteMapper;
import com.micro.ai.template.service.TemplateFavoriteService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 模板收藏Service实现
 *
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TemplateFavoriteServiceImpl extends ServiceImpl<TemplateFavoriteMapper, TemplateFavorite>
        implements TemplateFavoriteService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TemplateFavorite favoriteTemplate(String templateId, String userId) {
        log.info("收藏模板, templateId: {}, userId: {}", templateId, userId);

        // 检查是否已经收藏
        LambdaQueryWrapper<TemplateFavorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TemplateFavorite::getTemplateId, templateId)
                .eq(TemplateFavorite::getUserId, userId);
        
        TemplateFavorite existing = this.getOne(wrapper);
        if (existing != null) {
            log.warn("模板已收藏, templateId: {}, userId: {}", templateId, userId);
            return existing;
        }

        // 创建收藏记录
        TemplateFavorite favorite = new TemplateFavorite();
        favorite.setTemplateId(templateId);
        favorite.setUserId(userId);
        favorite.setCreatedAt(LocalDateTime.now());
        
        this.save(favorite);
        log.info("收藏成功, favoriteId: {}", favorite.getId());
        
        return favorite;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void unfavoriteTemplate(String templateId, String userId) {
        log.info("取消收藏, templateId: {}, userId: {}", templateId, userId);

        LambdaQueryWrapper<TemplateFavorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TemplateFavorite::getTemplateId, templateId)
                .eq(TemplateFavorite::getUserId, userId);
        
        boolean removed = this.remove(wrapper);
        if (!removed) {
            throw new BusinessException("收藏记录不存在");
        }
        
        log.info("取消收藏成功");
    }

    @Override
    public boolean isFavorited(String templateId, String userId) {
        LambdaQueryWrapper<TemplateFavorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TemplateFavorite::getTemplateId, templateId)
                .eq(TemplateFavorite::getUserId, userId);
        
        return this.count(wrapper) > 0;
    }

    @Override
    public IPage<TemplateFavorite> getUserFavorites(String userId, Page<TemplateFavorite> page) {
        LambdaQueryWrapper<TemplateFavorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TemplateFavorite::getUserId, userId)
                .orderByDesc(TemplateFavorite::getCreatedAt);
        
        return this.page(page, wrapper);
    }

    @Override
    public long getFavoriteCount(String templateId) {
        LambdaQueryWrapper<TemplateFavorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TemplateFavorite::getTemplateId, templateId);
        
        return this.count(wrapper);
    }
}

